<?php
   include 'cls_connection.php';
   class cls_user {
   var $connection;   

function getCurrentTopic()
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
		   $sql="SELECT MAX(topic_id) as max FROM topics";
		   $res = mysql_query($sql) or die($sql); 
		   $reg = mysql_fetch_array($res);
		   $id=$reg['max'];
		   $sql = "SELECT topic_name,description FROM topics where topic_id='$id'";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
   
function getComments2()
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql=   "SELECT  users.user_id, users.email as email ,comments.content as content FROM users ,comments  where users.user_id=comments.user_id";
			//$sql = "SELECT content FROM comments order by id desc";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}   
   
function getComments()
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "SELECT email,content FROM comments order by id desc";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}	

		function uploadComment($user_id,$comment)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO comments(user_id,email,content) VALUES ($user_id,(select email from users where user_id='$user_id'),'$comment')";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
	
	function userRegister($name,$lastName,$email,$password)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO users(name,last_name,email,password) VALUES ('$name,','$lastName','$email','$password')";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}


	function userUpdate($user_id,$name,$lastName,$email,$mobil)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "UPDATE users SET name='$name' ,last_name='$lastName' ,email='$email' ,mobil_num='$mobil' where user_id='$user_id'";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
	

function saveImage($user_id,$image_name,$jpg)
{
 $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO image_users(user_id,image_name,image) VALUES('$user_id','$image_name','$jpg')"; 

			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
		//$my_error = mysql_error($link);
		return $result;
     //$row = mysql_fetch_array($result);
     //row toma los balores que hay en el ringlon seleccionado
     //return $row["id"];                     //me debuelve el balor de la casilla typo en el registro seleccionado
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
	
function login ($admin_name,$password)
{
 $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
$sql = "SELECT * FROM administrator WHERE password = '$password' and admin_name='$admin_name'"; 

$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
 return $result;
     //$row = mysql_fetch_array($result);
     //row toma los balores que hay en el ringlon seleccionado
     //return $row["id"];                     //me debuelve el balor de la casilla typo en el registro seleccionado
	        } 
	      else
	       return -1;
mysql_free_result($sql);

}////////////////////////////////////////////

function loginUser ($email,$password)
{
 $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
$sql = "SELECT * FROM users WHERE email = '$email' and password='$password'"; 

$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
 return $result;
	        } 
	      else
	       return -1;
mysql_free_result($sql);

}


function getImageUser($user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM image_users WHERE user_id = '$user_id'"; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	 return $result;
		 //$row = mysql_fetch_array($result);
		 //row toma los balores que hay en el ringlon seleccionado
		 //return $row["id"];                     //me debuelve el balor de la casilla typo en el registro seleccionado
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}

function getImageTumbs($image_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM image_users WHERE image_id = '$image_id' "; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	 return $result;
		 //$row = mysql_fetch_array($result);
		 //row toma los balores que hay en el ringlon seleccionado
		 //return $row["id"];                     //me debuelve el balor de la casilla typo en el registro seleccionado
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}

function getUserData($user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM users WHERE user_id = '$user_id'"; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	 return $result;
		 //$row = mysql_fetch_array($result);
		 //row toma los balores que hay en el ringlon seleccionado
		 //return $row["id"];                     //me debuelve el balor de la casilla typo en el registro seleccionado
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}


function setImageProfile($image_id,$user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "update image_users  set image_profile='0' WHERE image_profile='1' and user_id=$user_id "; 
	
	$sql2 = "update image_users  set image_profile='1' WHERE image_id=$image_id  and user_id=$user_id"; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	$result2 = mysql_query($sql2) or die($sql2);   
	 //return $result;
		 //$row = mysql_fetch_array($result);
		 //row toma los balores que hay en el ringlon seleccionado
		 //return $row["id"];//me debuelve el balor de la casilla typo en el registro seleccionado
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}

function getImageProfile($user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM image_users WHERE user_id ='$user_id' and image_profile='1'"; 
	$result = mysql_query($sql) or die($sql); 
	 return $result;
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}



			  	  	 
}
?>